package com.ueffort.social.platform;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.ueffort.social.SocialProvider;
import com.ueffort.social.SocialSDK;
import com.ueffort.social.action.AuthAction;
import com.ueffort.social.action.RealAction;
import com.ueffort.social.action.ShareAction;
import com.ueffort.social.action.LeaderboardAction;
import com.ueffort.social.action.AchievementAction;
import com.ueffort.social.action.SocialAction;
import com.ueffort.social.inter.ShareObject;
import com.ueffort.social.utils.Apk;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FacebookSocialProvider implements SocialProvider{
    private static FacebookSocialProvider instance=new FacebookSocialProvider();

    private CallbackManager callbackManager;
    private Activity activity;

    private AuthAction authAction;
    private ShareAction shareAction;

    public static FacebookSocialProvider Instance(){
        return instance;
    }

    @Override
    public boolean isInstall() {
        return Apk.checkApkExist(activity, "com.facebook.katana");
    }

    @Override
    public boolean isShared() {
        return false;
    }

    @Override
    public boolean isLeaderboard() {
        return false;
    }

    @Override
    public boolean isAchievement() {
        return false;
    }

    @Override
    public boolean isReal() {
        return false;
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        return callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onCreate(Activity activity) {

    }

    @Override
    public void onDestroy(Activity activity) {

    }

    @Override
    public void Init(String appId, Activity activity, SocialAction action) {
        FacebookSdk.sdkInitialize(activity.getApplicationContext());
        this.activity = activity;
        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        if (authAction == null)return;
                        HashMap<String, String> data = new HashMap<>();
                        data.put("openId", loginResult.getAccessToken().getUserId());
                        data.put("accessToken", loginResult.getAccessToken().getToken());
                        data.put("nickName", Profile.getCurrentProfile().getName());

                        Log.e("facebook", Profile.getCurrentProfile().getName());
                        Log.e("facebook", Profile.getCurrentProfile().getId());
                        authAction.onComplete(SocialSDK.SocialPlatformType.Facebook, data);
                    }

                    @Override
                    public void onCancel() {
                        if (authAction == null)return;
                        authAction.onCancel(SocialSDK.SocialPlatformType.Facebook);
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        if (authAction == null)return;
                        authAction.onError(SocialSDK.SocialPlatformType.Facebook, exception);
                    }
                });
    }

    @Override
    public void Authorize(AuthAction action) {
        authAction = action;
        LoginManager.getInstance().logInWithReadPermissions(this.activity, Arrays.asList("public_profile"));
    }

    @Override
    public void AuthorizeStatus(AuthAction action) {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        boolean isLoggedIn = accessToken != null && !accessToken.isExpired();

        if (isLoggedIn){
            HashMap<String, String> data = new HashMap<>();
            data.put("openId", AccessToken.getCurrentAccessToken().getUserId());
            data.put("accessToken", AccessToken.getCurrentAccessToken().getToken());
            data.put("nickName", Profile.getCurrentProfile().getName());
            action.onComplete(SocialSDK.SocialPlatformType.Facebook, data);
        }else{
            action.onCancel(SocialSDK.SocialPlatformType.Facebook);
        }
    }

    @Override
    public void UpdateData(Map<String, String> info) {

    }

    @Override
    public void Real(RealAction action) {

    }

    @Override
    public void Logout(){

    }

    @Override
    public void ShareWithObject(ShareObject share, ShareAction action) {

    }

    @Override
    public void SubmitLeaderboard(String key, int value, LeaderboardAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Facebook);
    }

    @Override
    public void AddAchievement(String key, int value, AchievementAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Facebook);
    }
}